package com.ktg.mes.qc.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktg.common.core.domain.APIResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.qc.domain.QcItemTestingResultSort;
import com.ktg.mes.qc.service.IQcItemTestingResultSortService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 产品检测记录排序Controller
 *
 * @author byteco
 * @date 2023-01-31
 */
@RestController
@RequestMapping("/qc/qcItemTestingResultSort")
public class QcItemTestingResultSortController extends BaseController
{
    @Autowired
    private IQcItemTestingResultSortService qcItemTestingResultSortService;

    /**
     * 查询产品检测记录排序列表
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:list')")
    @GetMapping("/list")
    public Result<IPage<QcItemTestingResultSort>> list(QcItemTestingResultSort qcItemTestingResultSort,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询产品检测记录排序列表",req,null);
        try {
            Page<QcItemTestingResultSort> page = new Page<QcItemTestingResultSort>(pageNo, pageSize);
            IPage<QcItemTestingResultSort> pageList = qcItemTestingResultSortService.queryPageList(qcItemTestingResultSort,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出产品检测记录排序列表
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:export')")
    @Log(title = "产品检测记录排序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcItemTestingResultSort qcItemTestingResultSort,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出产品检测记录排序列表",req,qcItemTestingResultSort);
        try {
            List<QcItemTestingResultSort> list = qcItemTestingResultSortService.queryList(qcItemTestingResultSort);
            ExcelUtil<QcItemTestingResultSort> util = new ExcelUtil<QcItemTestingResultSort>(QcItemTestingResultSort.class);
            util.exportExcel(response, list, "产品检测记录排序数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取产品检测记录排序详细信息
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:query')")
    @GetMapping(value = "/{recordId}")
    public Result<QcItemTestingResultSort> getInfo(@PathVariable("recordId") Long recordId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取产品检测记录排序详细信息",req,null);
        try {
            return apiLog.ok(qcItemTestingResultSortService.getInfo(recordId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增产品检测记录排序
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:add')")
    @Log(title = "产品检测记录排序", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody QcItemTestingResultSort qcItemTestingResultSort,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增产品检测记录排序",req,qcItemTestingResultSort);
        try {
            QcItemTestingResultSort wmIssueHeader=new QcItemTestingResultSort();
            List<QcItemTestingResultSort> lqw =qcItemTestingResultSortService.queryList(wmIssueHeader);
            wmIssueHeader.setSortIndex(qcItemTestingResultSort.getSortIndex());
            List<QcItemTestingResultSort> lqw2 =qcItemTestingResultSortService.queryList(wmIssueHeader);
            if (lqw.size()>0){
                if (lqw2.size()>0){
                    for (int i = 0; i < lqw.size(); i++) {
                        if (Integer.valueOf(lqw.get(i).getSortIndex()) >= Integer.valueOf(qcItemTestingResultSort.getSortIndex())){
                            wmIssueHeader.setRecordId(lqw.get(i).getRecordId());
                            wmIssueHeader.setSortIndex(String.valueOf(Integer.valueOf(lqw.get(i).getSortIndex())+1));
                            qcItemTestingResultSortService.update(wmIssueHeader);
                        }
                    }
                }
            }
            qcItemTestingResultSortService.insert(qcItemTestingResultSort);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改产品检测记录排序
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:edit')")
    @Log(title = "产品检测记录排序", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody QcItemTestingResultSort qcItemTestingResultSort,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改产品检测记录排序",req,qcItemTestingResultSort);
        try {
            qcItemTestingResultSortService.update(qcItemTestingResultSort);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除产品检测记录排序
     */
    @PreAuthorize("@ss.hasPermi('qc:qcItemTestingResultSort:remove')")
    @Log(title = "产品检测记录排序", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result remove(@PathVariable Long[] recordIds, HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除产品检测记录排序",req,null);
        try {
                for (int j = 0; j < recordIds.length; j++) {
                    if (recordIds.length==1){
                        QcItemTestingResultSort wmIssueHeader=new QcItemTestingResultSort();
                        List<QcItemTestingResultSort> lqw =qcItemTestingResultSortService.queryList(wmIssueHeader);
                        wmIssueHeader.setRecordId(recordIds[j].longValue());
                        List<QcItemTestingResultSort> lqw2 =qcItemTestingResultSortService.queryList(wmIssueHeader);
                        if (lqw.size()>0){
                            for (int i = 0; i < lqw.size(); i++) {
                                if (Integer.valueOf(lqw.get(i).getSortIndex()) >= Integer.valueOf(lqw2.get(0).getSortIndex())){
                                    wmIssueHeader.setRecordId(lqw.get(i).getRecordId());
                                    wmIssueHeader.setSortIndex(String.valueOf(Integer.valueOf(lqw.get(i).getSortIndex())-1));
                                    qcItemTestingResultSortService.update(wmIssueHeader);
                                }
                            }
                        }
                    }

                }
            qcItemTestingResultSortService.delete(recordIds);
            return apiLog.ok("删除成功2！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
